<?php

/**
 * 下载管理
 * @author 陈德本
 *
 */
class downloadService extends commonService {

    /**
     * 下载主表
     *
     * @var downloadDao
     */
    protected $pao;

    /**
     * 下载分类
     *
     * @var downcatDao
     */
    protected $downcat;

    public function __construct() {
        parent::__construct();
        $this->pao = InitPHP::getDao("download");
        $this->downcat = InitPHP::getDao("downcat");
    }

    /**
     * 获取下载信息；$id=null则表示获取全部记录
     * @param string $id pid
     * @return Ambigous <multitype:, boolean, multitype:multitype: >
     */
    public function getDownload($id = NULL) {
        if ($id) {
            $map = array(
                'pid' => $id);
            return $this->pao->getAllByMutiple($map);
        } else {
            return $this->pao->getAllByMutiple();
        }
    }
    public function getList($pid, $start, $pernum)
    {
        $field = array(
            'pid' => $pid
        );
        $result = $this->pao->getList($field, $pernum, $start);
        $count = $this->pao->count($field);
        return array(
            $result,
            $count
        );
    }

    /**
     * 获取下载分类
     *
     * @param string $tree
     *            是否返回Tree
     * @return Ambigous <multitype:, multitype:array , boolean, multitype:multitype: >
     */
    public function getMenu($tree = TRUE) {
        $data = $this->downcat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'));
        if ($tree) {
            $data = list_to_tree($data, "id", "pid", "downloads");
        }
        return $data;
    }

    /**
     * 获取分类数组
     */
    public function getCatTree($flag = TRUE) {
        $data = $this->downcat->getAllByMutiple(array(), array(), array(
            'sort' => 'asc'));
        if (! $flag) {
            foreach ($data as $k => $v) {
                $result[$v['id']] = $v['title'];
            }
        } else {
            $data = list_to_tree($data, "id", "pid", "downloads");
            $result = array();
            if ($data) {
                foreach ($data as $k => $v) {
                    $result[$v['id']] = $v['title'];
                    if ($v['downloads']) {
                        foreach ($v['downloads'] as $k2 => $v2) {
                            $result[$v2['id']] = "&nbsp;&nbsp;|--&nbsp;&nbsp;" . $v2['title'];
                        }
                    }
                }
            }
        }
        return $result;
    }

    /**
     * 获取一条下载分类记录
     *
     * @param int $id            
     */
    public function getOneCat($id) {
        return $this->downcat->getOne($id);
    }

    /**
     * 添加一条下载分类
     *
     * @param array $data            
     */
    public function add_cat($data) {
        return $this->downcat->add($data);
    }

    /**
     * 删除一条下载分类
     *
     * @param int $id            
     * @return boolean
     */
    public function delete_cat($id) {
        return $this->downcat->delete($id);
    }

    /**
     * 更新一条下载分类记录
     *
     * @param int $id            
     * @param array $data            
     * @return boolean
     */
    public function update_cat($id, $data) {
        return $this->downcat->update($id, $data);
    }

}